package site.marqstree.kotlin.usercenter.arouter.interceptor

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import site.marqstree.kotlin.library.base.util.UserUtil
import site.marqstree.kotlin.library.provider.router.ProviderRouter


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.arouter.intercepter
 * 文件名: Loginterceptor
 * 创建者: marqstree
 * 创建时间: 2020/3/18 12:00
 * 描述: TODO
 */
@Interceptor(priority = 1)
class Loginterceptor: IInterceptor {
    var mContext: Context? = null

    /**
     * The operation of this interceptor.
     *
     * @param postcard meta
     * @param callback cb
     */
    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        //未登陆，跳转到登陆页面
        if (!UserUtil.isLogined()) {
            when(postcard.path){
                ProviderRouter.User.PATH_LOGIN ->{
                    callback.onContinue(postcard)
                }
                else ->{;
                    val bundle: Bundle = postcard.extras
                    // 被登录拦截了下来了
                    // 需要调转到登录页面，把参数跟被登录拦截下来的路径传递给登录页面，
                    // 登录成功后再跳转到被拦截的页面
                    ARouter.getInstance().build(ProviderRouter.User.PATH_LOGIN)
                        //有传递Bundle跟其他的属性Bundle必须写在前面否则其他的属性注入不了
                        .with(bundle)
                        .withString(ProviderRouter.NEXT_PATH, postcard.path)
                        .navigation()
                }
            }
        }
        // 如果已经登录不拦截
        else {
            callback.onContinue(postcard)
        }
    }

    /**
     * Do your init work in this method, it well be call when processor has been load.
     *
     * @param context ctx
     */
    override fun init(context: Context) {
        mContext = context
    }
}